package org.zecontech.plantvszombie.item;

import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.SpawnEggItem;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import org.zecontech.plantvszombie.init.ModItems;

public class PacketItem extends SpawnEggItem {

    private final int sunAmount;

    public PacketItem(EntityType<? extends Mob> entityType, Properties properties, int sunAmount) {
        super(entityType, properties);
        this.sunAmount = sunAmount;
    }

    @Override
    public InteractionResult useOn(UseOnContext useOnContext) {
        var player = useOnContext.getPlayer();
        if (player != null && player.getServer() != null) {
            if (player.getInventory().contains(new ItemStack(ModItems.SUN_ITEM))) {
                player.getInventory().forEach(stack -> {
                    if (stack.is(ModItems.SUN_ITEM) && stack.getCount() <= this.sunAmount) {
                        player.getServer().sendSystemMessage(Component.translatable("info.plant-vs-zombie.you_can_not_plant_it"));
                        return;
                    }
                });
            }
        }
        return super.useOn(useOnContext);
    }

    @Override
    public InteractionResult use(Level level, Player player, InteractionHand interactionHand) {
        if (player != null && player.getServer() != null) {
            if (player.getInventory().contains(new ItemStack(ModItems.SUN_ITEM))) {
                player.getInventory().forEach(stack -> {
                    if (stack.is(ModItems.SUN_ITEM) && stack.getCount() <= this.sunAmount) {
                        player.getServer().sendSystemMessage(Component.translatable("info.plant-vs-zombie.you_can_not_plant_it"));
                        return;
                    }
                });
            }
        }
        return super.use(level, player, interactionHand);
    }
}
